package com.workspace.js_website.controller;

import com.workspace.js_website.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 媒体文件相关控制
 */


@RestController
public class MediaController {

    private final MediaService mediaService;

    @Autowired
    public MediaController(MediaService mediaService) {
        this.mediaService = mediaService;
    }

    @GetMapping(value = "/image/{imgName}", produces = MediaType.IMAGE_JPEG_VALUE)
    public void getImage(@PathVariable("imgName") String imgName, HttpServletResponse response) throws IOException {
        String imgPath = "D:/lsn/jszx_resource/dataResourcesPicture/" + imgName;
        mediaService.getImage(imgPath,response);
    }

    @GetMapping(value = "/video/{videoName}", produces = MediaType.IMAGE_JPEG_VALUE)
    public void getVideo(@PathVariable("videoName") String videoName, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String videoPath = "D:/lsn/jszx_resource/CoreProductVideo/" + videoName;
        mediaService.getVideo(videoPath, request, response);
    }
}
